package com.easylinkin.linkappapi.concretestrength.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.concretestrength.entity.ConcreteStrengthInfo;
import com.easylinkin.linkappapi.concretestrength.entity.vo.ConcreteStrengthInfoDetailVo;
import com.easylinkin.linkappapi.concretestrength.entity.vo.ConcreteStrengthInfoVo;
import com.easylinkin.linkappapi.concretestrength.entity.vo.ConcreteStrengthVo;
import com.easylinkin.linkappapi.concretestrength.service.ConcreteStrengthInfoService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * ConcreteStrengthInfo表控制层
 *
 * @author zhengwen
 * @date 2022/10/23
 */
@Slf4j
@RestController
@RequestMapping("/concreteStrengthInfo")
public class ConcreteStrengthInfoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ConcreteStrengthInfoService concreteStrengthInfoService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<ConcreteStrengthInfo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ConcreteStrengthInfo> record = concreteStrengthInfoService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.concreteStrengthInfoService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appConcreteStrengthInfo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody ConcreteStrengthInfo appConcreteStrengthInfo) {
        return RestBuilders.successBuilder().success((this.concreteStrengthInfoService.saveOne(appConcreteStrengthInfo))).build();
    }

    /**
     * 修改数据
     *
     * @param appConcreteStrengthInfo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody ConcreteStrengthInfo appConcreteStrengthInfo) {
        return RestBuilders.successBuilder().success(this.concreteStrengthInfoService.updateOne(appConcreteStrengthInfo)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    @CommonOperateLogAnnotate(module = LogConstant.LogModule.QUALITY_MANAGEMENT, desc = "")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        //记录日志
        if(CollectionUtil.isNotEmpty(idList)){
            List<String> names = new ArrayList<>();
            List<String> times = new ArrayList<>();
            idList.stream().forEach(d->{
                //RestMessage rs = this.concreteStrengthInfoService.infoDto(d);
                //ConcreteStrengthInfoDetailVo dto = JSONUtil.toBean(JSONUtil.toJsonStr(rs.getData().toString()),ConcreteStrengthInfoDetailVo.class);
                ConcreteStrengthInfoDetailVo dto = concreteStrengthInfoService.getOneDtoById(d);
                if(dto != null){
                    names.add(dto.getMemberName());
                    times.add(DateUtil.format(dto.getCheckTime(), DatePattern.NORM_DATETIME_PATTERN));
                }
            });
            LogHelper.setContent(LogConstant.LogOperateType.SAFE_CONCRETE_STRENGTH_INFO_DELETE, StringUtil.join(names,","),StringUtil.join(times,","));
        }
        return RestBuilders.successBuilder().success(this.concreteStrengthInfoService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ConcreteStrengthInfo appConcreteStrengthInfo, HttpServletRequest request, HttpServletResponse response) {
        concreteStrengthInfoService.export(appConcreteStrengthInfo, request, response);
    }

    /**
     * 保持数据
     *
     * @param concreteStrengthVo 实体对象
     * @return 新增结果
     */
    @PostMapping("/saveCheckData")
    @ApiOperation("保持测量数据")
    public RestMessage saveCheckData(@RequestBody ConcreteStrengthVo concreteStrengthVo) {
        return concreteStrengthInfoService.saveCheckData(concreteStrengthVo);
    }

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/getInfoDtoPage")
    @ApiOperation("查询分页")
    public RestMessage getInfoDtoPage(@RequestBody RequestModel<ConcreteStrengthInfoVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        return concreteStrengthInfoService.selectInfoDtoPage(requestModel);
    }

    /**
     * 检测信息dto
     *
     * @param id info主键
     * @return
     */
    @GetMapping("/infoDto/{id}")
    @ApiOperation("检测信息dto")
    public RestMessage infoDto(@PathVariable Serializable id) {
        return concreteStrengthInfoService.infoDto(id);
    }

    /**
     * 检测信息dto先按测试数据类型分
     *
     * @param id 主键
     * @return 统一出参
     */
    @GetMapping("/infoTypeDto/{id}")
    @ApiOperation("检测信息dto先按测试数据类型分")
    public RestMessage infoTypeDto(@PathVariable Serializable id) {
        return concreteStrengthInfoService.infoTypeDto(id);
    }
}

